﻿using stdCall.MyUtils;
using System.Windows.Forms;

namespace stdCall.MyAnimate
{
    class Animate
    {
       /* static int defaultSpeed = 1;*/

        /// <summary>
        /// 移动动画
        /// </summary>
        /// <param name="label"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public  void moveTo(Label label, int x, int y)
        {
            while (label.Location.X != x || label.Location.Y != y)
            {
                if (label.Location.X < x)
                {
                    label.Location = new System.Drawing.Point(label.Location.X + 1, label.Location.Y);
                }
                else if (label.Location.X > x)
                {
                    label.Location = new System.Drawing.Point(label.Location.X - 1, label.Location.Y);
                }

                if (label.Location.Y < y)
                {
                    label.Location = new System.Drawing.Point(label.Location.X , label.Location.Y+1);
                }
                else if (label.Location.Y > y)
                {
                    label.Location = new System.Drawing.Point(label.Location.X , label.Location.Y-1);
                }
             //   Utils.Delay(1);
            }
        }

        /// <summary>
        /// 动画函数，实现Label切换
        /// </summary>
        /// <param name="labelOut">出去的Label</param>
        /// <param name="labelIn">进入的Label</param>
        /// <param name="panel">面板，用以获取大小</param>
        public void switchLabel(Label labelOut, Label labelIn, Panel panel)
        {
            panel.Controls.Add(labelIn);
            labelOut.Dock = System.Windows.Forms.DockStyle.None;
            labelIn.Dock = System.Windows.Forms.DockStyle.None;
            labelIn.Size = new System.Drawing.Size(panel.Size.Width, panel.Size.Height);
            labelOut.Size = new System.Drawing.Size(panel.Size.Width, panel.Size.Height);
            labelIn.Location = new System.Drawing.Point(labelOut.Location.X, labelOut.Location.Y - labelOut.Height);
            labelIn.Show();
            int offsety = 0;
            int firstInY = labelIn.Location.Y;
            int firstOutY = labelOut.Location.Y;
            while (offsety < labelOut.Height)
            {
                labelOut.Location = new System.Drawing.Point(labelOut.Location.X, firstOutY + offsety);
                labelIn.Location = new System.Drawing.Point(labelIn.Location.X, firstInY + offsety);
                offsety++;
                Utils.Delay(5);
            }
            labelOut.Dock = System.Windows.Forms.DockStyle.Fill;
            labelIn.Dock = System.Windows.Forms.DockStyle.Fill;
            labelOut.Location = new System.Drawing.Point(labelOut.Location.X, firstInY);
            labelOut.Hide();
        }


        /// <summary>
        /// 动画函数，实现Label切换
        /// </summary>
        /// <param name="labelOut"></param>
        /// <param name="labelIn"></param>
        /// <param name="panel"></param>
        /// <param name="speed">切换时间，越大越慢</param>
        public void switchLabel(Label labelOut, Label labelIn, Panel panel,int speed)
        {
            panel.Controls.Add(labelIn);
            //取消填充，否则动画有问题
            labelOut.Dock = System.Windows.Forms.DockStyle.None;
            labelIn.Dock = System.Windows.Forms.DockStyle.None;
            //调整大小
            labelIn.Size = new System.Drawing.Size(panel.Size.Width, panel.Size.Height);
            labelOut.Size = new System.Drawing.Size(panel.Size.Width, panel.Size.Height);
            //调整位置
            labelIn.Location = new System.Drawing.Point(panel.Location.X, panel.Location.Y - labelOut.Height);
            labelIn.Show();
            int offsety = 0;
            int firstInY = labelIn.Location.Y;
            int firstOutY = labelOut.Location.Y;
            int moveNum = labelOut.Height;
            int x = labelOut.Location.X;
            while (offsety < moveNum)
            {
                labelOut.Location = new System.Drawing.Point(x, firstOutY + offsety);
                labelIn.Location = new System.Drawing.Point(x, firstInY + offsety);
                offsety+=3;
                Utils.Delay(speed);
            }
            labelOut.Dock = System.Windows.Forms.DockStyle.Fill;
            labelIn.Dock = System.Windows.Forms.DockStyle.Fill;
            labelOut.Location = new System.Drawing.Point(labelOut.Location.X, firstInY);
            labelOut.Hide();
        }
    }
}
